package com.supersystem.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

/**
 * 生成json数据的时候会出现无法解释
 * 日期类型的情况，这里定义处理方法，处理日期类型
 * @author shamo
 *
 */
public class DateJsonValueProcessor implements JsonValueProcessor {
	private String format = "yyyy-MM-dd";
	
	public DateJsonValueProcessor(String format){
		this.format = format;
	}
	
	public DateJsonValueProcessor(){
	}
	
	public Object processArrayValue(Object arg0, JsonConfig arg1) {
		return this.process(arg0);
	}
	public Object processObjectValue(String arg0, Object arg1,JsonConfig arg2) {
		return this.process(arg1);
	}
	private Object process(Object value) {
		if (value == null) {
			return "";
		}else if (value instanceof Date){
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.format((Date) value);
		}else{
			return value.toString();
		}
	}
}